﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DebugAnts
{
    public partial class Form1 : Form
    {
        List<Turn> turns = new List<Turn>();
        ZoneManager zoneManager;
        int currentTurn;
        int cellSize = 20;

        public Form1()
        {
            InitializeComponent();
            currentTurn = 0;
            //ReadLog("AntLog.ant");
            string fileName = "";
            openFileDialog1.Filter = "Ant Log Files (.ant)|*.ant";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.InitialDirectory = "Z:\\INF781 - Intelligence Artifielle\\Projet\\AIChallenge\\Ants\\tools";
            openFileDialog1.FileName = "";
            btReadLog.Text = "Loading ...";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
            }
            ReadLog(fileName);
            //ReadLog("Z:\\INF781 - Intelligence Artifielle\\Projet\\AIChallenge\\Ants\\tools\\AntLog.ant");
            nUDCols.Maximum = Turn.sizeCols-1;
            nUDRow.Maximum = Turn.sizeRows-1;
            UpdateTurnButtons();
            UpdateGrid();
            this.KeyPreview = true;
        }

        private void cbAlliedAnts_CheckedChanged(object sender, EventArgs e)
        {
            if(cbAlliedAnts.Checked)
            {
                cbHunter.Enabled = true;
                cbTrooper.Enabled = true;
                cbDefender.Enabled = true;
                cbHunter.Checked = true;
                cbTrooper.Checked = true;
                cbDefender.Checked = true;
            }
            else
            {
                cbHunter.Enabled = false;
                cbTrooper.Enabled = false;
                cbDefender.Enabled = false;
                cbHunter.Checked = false;
                cbTrooper.Checked = false;
                cbDefender.Checked = false;
            }
            UpdateGrid();
        }

        private void btReadLog_Click(object sender, EventArgs e)
        {
            string fileName = "";
            openFileDialog1.Filter = "Ant Log Files (.ant)|*.ant";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.InitialDirectory = "Z:\\INF781 - Intelligence Artifielle\\Projet\\AIChallenge\\Ants\\tools";
            openFileDialog1.FileName = "";
            btReadLog.Text = "Loading ...";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
            }
            ReadLog(fileName);
            currentTurn = 0;
            nUDCols.Maximum = Turn.sizeCols-1;
            nUDRow.Maximum = Turn.sizeRows-1;
            UpdateTurnButtons();
            UpdateGrid();
            btReadLog.Text = "Read AntLog.ant";
            this.Focus();
        }

        private void btFirstTurn_Click(object sender, EventArgs e)
        {
            currentTurn = 0;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void btMinus100Turns_Click(object sender, EventArgs e)
        {
            currentTurn -= 100;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void btMinus10Turns_Click(object sender, EventArgs e)
        {
            currentTurn -= 10;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void btMinus1Turn_Click(object sender, EventArgs e)
        {
            currentTurn -= 1;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void btPlus1Turn_Click(object sender, EventArgs e)
        {
            currentTurn += 1;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void btPlus10Turns_Click(object sender, EventArgs e)
        {
            currentTurn += 10;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void btPlus100Turns_Click(object sender, EventArgs e)
        {
            currentTurn += 100;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void btLastTurn_Click(object sender, EventArgs e)
        {
            currentTurn = turns.Count - 1;
            UpdateTurnButtons();
            UpdateGrid();
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdateTurnButtons()
        {
            zoneManager.SetTurnZone(turns[currentTurn]);
            lbTurn.Text = "Turn: " + (currentTurn + 1).ToString() + " / " + turns.Count.ToString();
            lTurnDuration.Text = "Turn Duration : " + turns[currentTurn].turnTimeMS.ToString() + " ms";
            lBirthDuration.Text = "Birth Duration : " + turns[currentTurn].birthTimeMS.ToString() + " ms";
            lViewDuration.Text = "View Duration : " + turns[currentTurn].updateViewTimeMS.ToString() + " ms";
            lBehaviorDuration.Text = "Behavior Duration : " + turns[currentTurn].behaviorTimeMS.ToString() + " ms";
            if(currentTurn - 100 < 0)
            {
                btMinus100Turns.Enabled = false;
            }
            else
            {
                btMinus100Turns.Enabled = true;
            }
            if (currentTurn - 10 < 0)
            {
                btMinus10Turns.Enabled = false;
            }
            else
            {
                btMinus10Turns.Enabled = true;
            }
            if (currentTurn - 1 < 0)
            {
                btMinus1Turn.Enabled = false;
            }
            else
            {
                btMinus1Turn.Enabled = true;
            }
            if (currentTurn + 1 > turns.Count-1)
            {
                btPlus1Turn.Enabled = false;
            }
            else
            {
                btPlus1Turn.Enabled = true;
            }
            if (currentTurn + 10 > turns.Count - 1)
            {
                btPlus10Turns.Enabled = false;
            }
            else
            {
                btPlus10Turns.Enabled = true;
            }
            if (currentTurn + 100 > turns.Count - 1)
            {
                btPlus100Turns.Enabled = false;
            }
            else
            {
                btPlus100Turns.Enabled = true;
            }
        }

        private void pbMapGrid_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                Graphics g = e.Graphics;
                int numOfRows = Turn.sizeRows;
                int numOfCols = Turn.sizeCols;
      
                Pen p = new Pen(Color.Black,2);
                pbMapGrid.Size = new Size((cellSize + 2) * (numOfCols), (cellSize + 2) * (numOfRows));

                Font font = new Font("Arial", 10.0f);
                for (int y = 0; y < numOfRows + 2; ++y)
                {
                    g.DrawLine(p, 0, y * cellSize, (numOfCols+1) * cellSize, y * cellSize);
                    g.DrawString((y-1).ToString(), font, Brushes.Black, new Point(0, y * cellSize));
                }
                for (int x = 0; x < numOfCols + 2; ++x)
                {
                    g.DrawLine(p, x * cellSize, 0, x * cellSize, (numOfRows+1) * cellSize);
                    g.DrawString((x-1).ToString(), font, Brushes.Black, new Point(x * cellSize, 0));
                }

                Square square;
                Square afterSquare;
                SolidBrush blackBrush = new SolidBrush(Color.FromArgb(255, 30, 30, 30)); //Undiscovered
                SolidBrush blueBrush = new SolidBrush(Color.CornflowerBlue); //Water
                SolidBrush brownBrush = new SolidBrush(Color.Peru); //Ground
                SolidBrush greenBrush = new SolidBrush(Color.Green); //Allied Hill
                SolidBrush orangeBrush = new SolidBrush(Color.Orange); //Enemy Hill
                SolidBrush turquoiseBrush = new SolidBrush(Color.Turquoise); //Allied Hunter
                SolidBrush purpleBrush = new SolidBrush(Color.MediumOrchid); //Allied Trooper
                SolidBrush whiteBrush = new SolidBrush(Color.White); //Allied Defender
                SolidBrush redBrush = new SolidBrush(Color.Red); //Enemy
                SolidBrush yellowBrush = new SolidBrush(Color.Yellow); //Food
                SolidBrush grayBrush = new SolidBrush(Color.FromArgb(180, 100, 100, 100)); //Fog Of War
                for (int y = 1; y < numOfRows + 1; ++y)
                {
                    for (int x = 1; x < numOfCols + 1; ++x)
                    {
                        Rectangle circle = new Rectangle((x * cellSize) + (int)Math.Ceiling(cellSize / 4.0), (y * cellSize) + (int)Math.Ceiling(cellSize / 4.0), cellSize - (int)Math.Ceiling(cellSize / 2.0), cellSize - (int)Math.Ceiling(cellSize / 2.0));
                        Rectangle rectangle = new Rectangle((x * cellSize) + 1, (y * cellSize) + 1, cellSize - 2, cellSize - 2);
                        square = turns[currentTurn].grid[y-1][x-1];
                        if(currentTurn > 0)
                        {
                            afterSquare = turns[currentTurn - 1].grid[y - 1][x - 1];
                        }
                        else
                        {
                            afterSquare = square;
                        }
                        if(!square.isDiscovered)
                        {
                            g.FillRectangle(blackBrush, rectangle);
                        }
                        else if(square.isWater)
                        {
                            g.FillRectangle(blueBrush, rectangle);
                        }
                        else
                        {
                            if(square.hill == -1)
                            {
                                 g.FillRectangle(brownBrush, rectangle);
                            }
                            else if(square.hill > 0)
                            {
                                 g.FillRectangle(orangeBrush, rectangle);
                            }
                            else
                            {
                                 g.FillRectangle(greenBrush, rectangle);
                            }
                            if((square.isFood) && cbFood.Checked)
                            {
                                g.FillEllipse(yellowBrush, circle);
                            }
                            else if((square.ant > 0) && cbEnemyAnts.Checked)
                            {
                                g.FillEllipse(redBrush, circle);
                            }
                            else if ((afterSquare.ant == 0) && (square.allyInfluence == 100000) && cbAlliedAnts.Checked)
                            {
                                if ((afterSquare.antType == 1) && cbHunter.Checked) //Hunter
                                {
                                    g.FillEllipse(turquoiseBrush, circle);
                                }
                                else if ((afterSquare.antType == 2) && cbTrooper.Checked) //Trooper
                                {
                                    g.FillEllipse(purpleBrush, circle);
                                }
                                else if ((afterSquare.antType == 3) && cbDefender.Checked) //Defender
                                {
                                    g.FillEllipse(whiteBrush, circle);
                                }
                            }
                        }
                        if(!square.isVisible && square.isDiscovered && cbFogOfWar.Checked)
                        {
                            g.FillRectangle(grayBrush, rectangle);
                        }
                        if(!rbNone.Checked)
                        {
                            //À REVOIR
                            Pen olivePen = new Pen(Color.FromArgb(GetScentToAlpha(x - 1, y - 1, 255), 173, 255, 47), 2); //Scent
                            g.DrawRectangle(olivePen, rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2);
                        }
                    }
                }
                if (cbZones.Checked)
                {
                    Pen redPen = new Pen(Color.FromArgb(128, 255, 0, 0), 1);
                    for (int y = 0; y < zoneManager.nbZoneRows; ++y)
                    {
                        for (int x = 0; x < zoneManager.nbZoneCols; ++x)
                        {
                            int row = (y * zoneManager.zoneSize) + 1;
                            int col = (x * zoneManager.zoneSize) + 1;
                            g.DrawRectangle(redPen, (col * cellSize) + 1, (row * cellSize) + 1, cellSize * zoneManager.zoneSize - 2, cellSize * zoneManager.zoneSize - 2);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
            }
        }

        private int GetScentToAlpha(int x, int y, int max)
        {
            float diffusionValue = 0.15f; //0.25f
            double logType = Math.Log(1.0f / diffusionValue);
            int scent = 0;
            if (rbFood.Checked)
            {
                scent = (int)Math.Ceiling((((Math.Log(turns[currentTurn].grid[y][x].foodScent) / logType) - 1) / (Math.Log(turns[currentTurn].maxTurnScent) / logType)) * max);
            }
            else if(rbExploration.Checked)
            {
                diffusionValue = 0.1f; //0.2f
                logType = Math.Log(1.0f / diffusionValue);
                if (turns[currentTurn].grid[y][x].unexploredScent == 100000)
                {
                    scent = 0;
                }
                else
                {
                    scent = (int)Math.Ceiling((((Math.Log(turns[currentTurn].grid[y][x].unexploredScent) / logType) - 1) / (Math.Log(turns[currentTurn].maxTurnScent) / logType)) * max);
                }
            }
            else if(rbUnvisible.Checked)
            {
                diffusionValue = 0.1f; //0.2f
                logType = Math.Log(1.0f / diffusionValue);
                if (turns[currentTurn].grid[y][x].unVisibleScent == 100000)
                {
                    scent = 0;
                }
                else
                {
                    scent = (int)Math.Ceiling((((Math.Log(turns[currentTurn].grid[y][x].unVisibleScent) / logType) - 1) / (Math.Log(turns[currentTurn].maxTurnScent) / logType)) * max);
                }
            }
            else if (rbEnemyInfluence.Checked)
            {
                scent = (int)Math.Ceiling((((Math.Log(turns[currentTurn].grid[y][x].enemyInfluence) / logType) - 1) / (Math.Log(turns[currentTurn].maxTurnScent) / logType)) * max);
            }
            else if (rbEnemyHillInfluence.Checked)
            {
                scent = (int)Math.Ceiling((((Math.Log(turns[currentTurn].grid[y][x].enemyHillInfluence) / logType) - 1) / (Math.Log(turns[currentTurn].maxTurnScent) / logType)) * max);
            }
            else if (rbAllyInfluence.Checked)
            {
                scent = (int)Math.Ceiling((((Math.Log(turns[currentTurn].grid[y][x].allyInfluence) / logType) - 1) / (Math.Log(turns[currentTurn].maxTurnScent) / logType)) * max);
            }
            else if (rbAllyHillInfluence.Checked)
            {
                //scent = (int)Math.Ceiling((((Math.Log(turns[currentTurn].grid[y][x].allyHillInfluence) / logType) - 1) / (Math.Log(turns[currentTurn].maxTurnScent) / logType)) * max);
                if((int)Math.Ceiling(turns[currentTurn].grid[y][x].allyHillInfluence) > 0.0f)
                {
                    scent = max;
                }
            }
            else if (rbDanger.Checked)
            {
                //À MODIFIER
                //scent = (turns[currentTurn].grid[y][x].foodScent * 6);
            }
            if(scent < 0)
            {
                scent = 0;
            }
            else if(scent > max)
            {
                scent = max;
            }
            return scent;
        }

        private void UpdateGrid()
        {
            pbMapGrid.Refresh();
            SetDetails();
        }

        private void ReadLog(string fileName)
        {
            try
            {
                string[] lines = System.IO.File.ReadAllLines(fileName);
                int sizeRows = 0, sizeCols = 0;
                int zoneSize = 0;
                int turnNumber = -1;
                float maxTurnScent =  0.0f;
                double turnTimeMS = 0.0, birthTimeMS = 0.0, updateViewTimeMS = 0.0, behaviorTimeMS = 0.0, setupTimeMS = 0.0;
                int nbHunter = 0, nbTrooper = 0, nbDefender = 0;
                List<List<Square>> grid = new List<List<Square>>();
                List<Square> row;
                turns.Clear();
                foreach (string line in lines)
                {
                    ++turnNumber;
                    if (turnNumber == 0)
                    {
                        string[] sizes = line.Split(';');
                        sizeRows = int.Parse(sizes[0]);
                        sizeCols = int.Parse(sizes[1]);
                        zoneSize = int.Parse(sizes[2]);
                        setupTimeMS = double.Parse(sizes[3], System.Globalization.CultureInfo.InvariantCulture);
                        lSetupTime.Text = "Setup Time : " + setupTimeMS.ToString() + "ms"; 
                        zoneManager = new ZoneManager(zoneSize, sizeRows, sizeCols);
                    }
                    else
                    {
                        string[] data = line.Split('#');
                        //Info
                        string[] info = data[0].Split(';');
                        turnTimeMS = double.Parse(info[0], System.Globalization.CultureInfo.InvariantCulture);
                        birthTimeMS = double.Parse(info[1], System.Globalization.CultureInfo.InvariantCulture);
                        updateViewTimeMS = double.Parse(info[2], System.Globalization.CultureInfo.InvariantCulture);
                        behaviorTimeMS = double.Parse(info[3], System.Globalization.CultureInfo.InvariantCulture);
                        nbHunter = int.Parse(info[4]);
                        nbTrooper = int.Parse(info[5]);
                        nbDefender = int.Parse(info[6]);

                        //Squares
                        row = new List<Square>();
                        Square square;
                        maxTurnScent = 0.0f;
                        string[] squareText;
                        string[] squares = data[1].Split('|');

                        //De gauche à droite puis de haut en bas
                        for (int y = 0; y < sizeRows; ++y)
                        {
                            for (int x = 0; x < sizeCols; ++x)
                            {
                                squareText = squares[(y * sizeCols) + x].Split(';');
                                square = new Square();
                                square.ant = int.Parse(squareText[0]);
                                square.hill = int.Parse(squareText[1]);
                                square.antType = int.Parse(squareText[2]);
                                square.foodScent = float.Parse(squareText[3], System.Globalization.CultureInfo.InvariantCulture);
                                square.unexploredScent = float.Parse(squareText[4], System.Globalization.CultureInfo.InvariantCulture);
                                square.unVisibleScent = float.Parse(squareText[5], System.Globalization.CultureInfo.InvariantCulture);
                                square.unexploredTimer = int.Parse(squareText[6]);
                                square.enemyInfluence = float.Parse(squareText[7], System.Globalization.CultureInfo.InvariantCulture);
                                square.enemyHillInfluence = float.Parse(squareText[8], System.Globalization.CultureInfo.InvariantCulture);
                                square.allyInfluence = float.Parse(squareText[9], System.Globalization.CultureInfo.InvariantCulture);
                                square.allyHillInfluence = float.Parse(squareText[10], System.Globalization.CultureInfo.InvariantCulture);
                                square.isWater = Convert.ToBoolean(int.Parse(squareText[11]));
                                square.isVisible = Convert.ToBoolean(int.Parse(squareText[12]));
                                square.isDiscovered = Convert.ToBoolean(int.Parse(squareText[13]));
                                square.isFood = Convert.ToBoolean(int.Parse(squareText[14]));
                                maxTurnScent = Math.Max(maxTurnScent, square.allyInfluence);
                                row.Add(square);
                            }
                            grid.Add(row);
                            row = new List<Square>();
                        }
                        turns.Add(new Turn(turnNumber, turnTimeMS, birthTimeMS, updateViewTimeMS, behaviorTimeMS, nbHunter, nbTrooper, nbDefender, maxTurnScent, grid));
                        grid = new List<List<Square>>();
                    }
                }
                Turn.sizeRows = sizeRows;
                Turn.sizeCols = sizeCols;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
        }

        private void bDetails_Click(object sender, EventArgs e)
        {
            SetDetails();
        }

        private void SetDetails()
        {
            lCol.Text = nUDCols.Value.ToString();
            lRow.Text = nUDRow.Value.ToString();
            lNbHunter.Text = turns[currentTurn].nbHunter.ToString();
            lNbTrooper.Text = turns[currentTurn].nbTrooper.ToString();
            lNbDefender.Text = turns[currentTurn].nbDefender.ToString();
            lFoodScent.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].foodScent.ToString();
            lUnexploredScent.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].unexploredScent.ToString();
            lUnvisibleScent.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].unVisibleScent.ToString();
            lUnexploredTimer.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].unexploredTimer.ToString();
            lAllyInfluence.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].allyInfluence.ToString();
            lAllyHillInfluence.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].allyHillInfluence.ToString();
            lEnemyInfluence.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].enemyInfluence.ToString();
            lEnemyHillInfluence.Text = turns[currentTurn].grid[(int)nUDRow.Value][(int)nUDCols.Value].enemyHillInfluence.ToString();

            //Zones
            int zoneRow = (int)Math.Ceiling(((nUDRow.Value + 1) / zoneManager.zoneSize)) - 1;
            int zoneCol = (int)Math.Ceiling(((nUDCols.Value + 1) / zoneManager.zoneSize)) - 1;
            lSelectedZone.Text = "(" + zoneCol.ToString() + ", " + zoneRow.ToString() + ")";
            lAllyZoneValue.Text = zoneManager.zones[zoneRow][zoneCol].allyValue.ToString();
            lEnemyZoneValue.Text = zoneManager.zones[zoneRow][zoneCol].enemyValue.ToString();
        }

        private void pbMapGrid_MouseClick(object sender, MouseEventArgs e)
        {
            Point p1 = pbMapGrid.PointToClient(Cursor.Position);
            int col = (int)(p1.X / cellSize - 1);
            int row = (int)(p1.Y / cellSize - 1);
            lHoverGrid.Text = "( " + col.ToString() + ", " + row.ToString() + ")";
            if(col < Turn.sizeCols && col >= 0)
            {
                nUDCols.Value = col;
            }
            else 
            {
                nUDRow.Value = Turn.sizeRows - 1;
            }
            if(row < Turn.sizeRows && row >= 0)
            {
                nUDRow.Value = row;
            }
            else 
            {
                nUDRow.Value = Turn.sizeRows - 1;
            }
            SetDetails();
        }

        private void pbMapGrid_MouseWheel(object sender, MouseEventArgs e)
        {
            /*if ((e.Delta > 0) && (cellSize + 5 <= 30))
            {
                cellSize += 5;
            }
            else if ((e.Delta < 0) && (cellSize - 5 >= 10))
            {
                cellSize -= 5;
            }
            UpdateGrid();*/
        }

        #region Changes on options
        private void cbHunter_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void cbTrooper_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void cbDefender_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void cbEnemyAnts_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void cbFood_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void cbFogOfWar_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbFood_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbExploration_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbEnemyInfluence_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbEnemyHillInfluence_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbAllyInfluence_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbAllyHillInfluence_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbDanger_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void rbNone_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void cbZones_CheckedChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }
        #endregion

        private void ZoomIn()
        {
            if (cellSize + 5 <= 30)
            {
                cellSize += 5;
            }
            if (cellSize + 5 > 30)
            {
                bZoomIn.Enabled = false;
            }
            if (cellSize - 5 >= 10)
            {
                bZoomOut.Enabled = true;
            }
            UpdateGrid();
        }

        private void ZoomOut()
        {
            if (cellSize - 5 >= 10)
            {
                cellSize -= 5;
            }
            if (cellSize - 5 < 10)
            {
                bZoomOut.Enabled = false;
            }
            if (cellSize + 5 <= 30)
            {
                bZoomIn.Enabled = true;
            }
            UpdateGrid();
        }
        
        private void bZoomIn_Click(object sender, EventArgs e)
        {
            ZoomIn();
        }

        private void bZoomOut_Click(object sender, EventArgs e)
        {
            ZoomOut();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //e.Handled = false;
            if ((e.KeyCode == Keys.NumPad4) && btMinus1Turn.Enabled)
            {
                currentTurn -= 1;
                UpdateTurnButtons();
                UpdateGrid();
            }
            else if ((e.KeyCode == Keys.NumPad6) && btPlus1Turn.Enabled)
            {
                currentTurn += 1;
                UpdateTurnButtons();
                UpdateGrid();
            }
            else if ((e.KeyCode == Keys.NumPad8) && bZoomIn.Enabled)
            {
                ZoomIn();
            }
            else if ((e.KeyCode == Keys.NumPad5) && bZoomOut.Enabled)
            {
                ZoomOut();
            }
        }
    }
}
